﻿
/* 
 * Since 2008 - 2013
 */
package com.hh.bss.oa.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.oa.dao.*;
import com.hh.bss.oa.entity.*;
import com.hh.bss.oa.service.*;
import com.hh.bss.oa.vo.query.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class OaMeetingRoomLogController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private OaMeetingRoomLogService oaMeetingRoomLogService;
	private static final String CONTENT_EXCEL_TYPE = "application/vnd.ms-excel";
	private OaMeetingRoomLog oaMeetingRoomLog;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setOaMeetingRoomLogServiceImpl(OaMeetingRoomLogService service) {
		this.oaMeetingRoomLogService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,OaMeetingRoomLogQuery oaMeetingRoomLogQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = oaMeetingRoomLogService.findPage(oaMeetingRoomLogQuery);
		
		List<OaMeetingRoomLog> OaMeetingRoomLoglist = (List) page.getResult();
		ListRange<OaMeetingRoomLog> resultList = new ListRange<OaMeetingRoomLog>();
		resultList.setRows(OaMeetingRoomLoglist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void down(HttpServletRequest request,HttpServletResponse response,OaMeetingRoomLogQuery oaMeetingRoomLogQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			if (onlineUser!=null)oaMeetingRoomLogQuery.setOnlineUser(onlineUser);
			
		}
		//Page page = accountReportService.workKpi(reportQuery);
		
		List list=oaMeetingRoomLogService.findList(oaMeetingRoomLogQuery);
		
		Map m=null;
		//处理字典数据
		
		for(int i=0;list!=null&&i<list.size();i++){
			m=(Map)list.get(i);
			m.put("useTypeName", ConstantDic.getConstantDicCategoryValue(ConstantDic.MeetingRoomUseType, ObjectUtils.toString(m.get("useType"))));
			m.put("statusName", ConstantDic.getConstantDicCategoryValue(ConstantDic.MeetingRoomOrderStatus, ObjectUtils.toString(m.get("status"))));			
		}
		
		try{
       		String title=new String(("会议室使用记录.xls").getBytes("GBK"),"iso8859-1"); 
       		//HttpServletResponse response=ServletActionContext.getResponse();
       		response.setHeader("Content-Disposition", "attachment;filename=\""+title+"\"");
       		response.setContentType(CONTENT_EXCEL_TYPE);
       		ServletOutputStream os = response.getOutputStream();
       		Map titles=new LinkedHashMap();
       		//姓名	BD call	BD call完成	客户拜访	客户拜访完成	面试候选人	面试候选人完成	推荐候选人给客户	推荐候选人给客户完成	有效简历输入	有效简历输入完成	KPI 总分数
       		//'account','name','bdCallCount','bdCallCountFin','customerVisitCount','customerVisitCountFin','giCount','giCountFin','recomCount','recomCountFin','cvCount','cvCountFin'
       		titles.put("branchCompanyName", "所在分公司");
       		titles.put("roomName", "会议室");
       		titles.put("useTypeName", "使用类型");
       		titles.put("title", "会议主题");
       		titles.put("orderDt", "预订时间");
       		titles.put("orderAccount", "预订人");
       		
       		titles.put("meetingDateString", "会议日期");       		
       		titles.put("beginDtString", "开始时间");
       		titles.put("endDtString", "结束时间");
       		
       		titles.put("statusName", "状态");
       		
       		
       		HSSFWorkbook wb = ExcelHelper.createExcelWorkbook("会议室使用记录", list, titles);
       		wb.write(os);
       		os.flush();
       		os.close();  
       	}catch(Exception ex){
       		 ex.printStackTrace();
       	}
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,OaMeetingRoomLog oaMeetingRoomLog) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomLog.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oaMeetingRoomLog,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomLog.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oaMeetingRoomLog,"createDt",new Date());}catch(Exception ex){}
				}

			oaMeetingRoomLogService.save(oaMeetingRoomLog);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,OaMeetingRoomLog oaMeetingRoomLog) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        OaMeetingRoomLog oldoaMeetingRoomLog = (OaMeetingRoomLog)oaMeetingRoomLogService.getById(oaMeetingRoomLog.getId());
                         
			bind(request,oldoaMeetingRoomLog);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomLog.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldoaMeetingRoomLog,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomLog.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldoaMeetingRoomLog,"modifiedDt",new Date());}catch(Exception ex){}
				}


			oaMeetingRoomLogService.update(oldoaMeetingRoomLog);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				oaMeetingRoomLogService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
